package com.liu.nb.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.liu.nb.entity.Circuitry;
import com.liu.nb.entity.CircuitryPole;
import com.liu.nb.util.Result;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 线路表 前端控制器
 * </p>
 *
 * @author yzl,zk 0907追加方法
 * @since 2021-08-31
 */
@RestController
@RequestMapping("/nb/circuitry")
public class CircuitryController extends BaseController{

    /**
     * 查询需要巡检的所有线路
     */
    @PreAuthorize("hasAnyRole('root','xianluadmin')")
    @PostMapping("/selLineName")
    public Result selLineName() {
        List<Circuitry> circuitryList = iCircuitryService.list();
        return Result.success(circuitryList);
    }

    /**
     * 根据线路id获取开始塔杆和终止塔杆====根据线路管理员通过选择线路名称下拉列表的id
     */
    @PreAuthorize("hasRole('xianluadmin')")
    @GetMapping("/selectById")
    public Result selectById(String id) {
        Circuitry circuitry = iCircuitryService.selCircuitry(id);
        return Result.success(circuitry);
    }
    //查询线路列表
    @PostMapping("/getall/{pageNo}/{pageSize}/{id}/{state}")
    public Result getAll(@PathVariable Integer pageNo,@PathVariable Integer pageSize,
                         @PathVariable Integer id,@PathVariable Integer state){
        return Result.success(iCircuitryService.getAll(pageNo,pageSize,id,state));
    }
    //增加线路
    @PreAuthorize("hasAnyRole('root','xianluadmin')")
    @PostMapping("/add/{poleIds}")
    public Result add(@RequestBody Circuitry circuitry,@PathVariable String[] poleIds){
        addPoleCir(circuitry.getId(),poleIds);
        return Result.success(iCircuitryService.save(circuitry));
    }
    //修改线路
    @PreAuthorize("hasAnyRole('root','xianluadmin')")
    @PostMapping("/upd/{poleIds}")
    public Result upd(@RequestBody Circuitry circuitry,@PathVariable String[] poleIds){
        addPoleCir(circuitry.getId(),poleIds);
        return Result.success(iCircuitryService.updateById(circuitry));
    }
    //更新启用状态
    @PreAuthorize("hasAnyRole('root','xianluadmin')")
    @GetMapping("/updstate/{id}")
    public Result updState(@PathVariable String id){
        Circuitry circuitry = iCircuitryService.selCircuitry(id);
        if(circuitry.getStartedStatus()==0){
            circuitry.setStartedStatus(1);
        }else {
            circuitry.setStartedStatus(0);
        }
        return Result.success(iCircuitryService.updateById(circuitry));
    }
    //删除
    @PreAuthorize("hasAnyRole('root','xianluadmin')")
    @GetMapping("/del/{id}")
    public Result del(@PathVariable String id){
        return Result.success(iCircuitryService.removeById(id));
    }
    //塔杆和线路的中间表添加数据
    public void addPoleCir( String circuitryId,String[] poleIds){
        if(poleIds.length>0){
            //删除原来的数据，插入新数据
            circuitryPoleService.remove(new QueryWrapper<CircuitryPole>().eq("circuity_id",circuitryId));
            int x=0;
            for (int i=0;i<poleIds.length;i++){
                 x=x+10;
                CircuitryPole circuitryPole=new CircuitryPole();
                circuitryPole.setPoleId(poleIds[i]);
                circuitryPole.setCircuityId(circuitryId);
                circuitryPole.setPosition(x);
                circuitryPoleService.save(circuitryPole);
            }
        }
    }


}
